package henkolib.events;

/**
 * Represents an Event with customizable arguments.
 * Should be kept private in the class that invokes it.
 * Use the IPublicSimpleEvent interface for publishing the event.
 * @author Henko
 */

class Event<T> implements IPublicEvent<T>
{
	var subscribed : Array<EventArgs<T>->Void>;
	
	public function new() 
	{
		subscribed = new Array<EventArgs<T>->Void>();
	}
	
	public function Subscribe(func:EventArgs<T>->Void) : Void
	{
		if (Reflect.isFunction(func))
		{
			subscribed.push(func);
		}
	}
	
	public function Unsubscribe(func:EventArgs<T>->Void) : Void
	{
		if (Reflect.isFunction(func))
		{
			subscribed.remove(func);
		}
	}
	
	public function Invoke(args : EventArgs<T>)
	{
		var callargs = new Array<Dynamic>();
		callargs.push(args);
		
		for (func in subscribed)
		{
			Reflect.callMethod( { }, func, callargs);
		}
	}
	
}